package cn.pw.test.com.news.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.aries.ui.view.title.TitleBarView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

import cn.pw.test.com.news.R;
import cn.pw.test.com.news.adapter.VideoAdapter;
import cn.pw.test.com.news.bean.VideoBean;
import cn.pw.test.com.news.manager.NewsPathManger;
import cn.pw.test.com.news.server.NewsBack;
import cn.pw.test.com.news.server.NewsGlobal;
import tech.com.commoncore.base.BaseTitleActivity;

@Route(path = NewsPathManger.NEWS_VIDEO)
public class VideoActivity extends BaseTitleActivity {

    private SmartRefreshLayout smartRefresh;
    private RecyclerView contentRecycler;

    private VideoAdapter videoAdapter;

    private List<VideoBean> videoBeans;

    @Override
    public void setTitleBar(TitleBarView titleBar) {
        titleBar.setTitleMainText(R.string.news_string_video_news_title);
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_video;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        smartRefresh = findViewById(R.id.smart_refresh_layout);
        contentRecycler = findViewById(R.id.content_recycler_view);

        videoAdapter = new VideoAdapter(R.layout.news_layout_item_video);
        contentRecycler.setLayoutManager(new LinearLayoutManager(mContext));
        contentRecycler.setAdapter(videoAdapter);

        smartRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                requestData();
            }
        });

        if (videoBeans == null) {
            smartRefresh.autoRefresh();
        }
    }

    private void requestData() {
        NewsGlobal.getNewsImp().requestVideo(0, 30, new NewsBack<List<VideoBean>>() {
            @Override
            public void onBack(List<VideoBean> beans, String err) {
                smartRefresh.finishRefresh();
                if (beans != null) {
                    videoBeans = beans;
                    videoAdapter.replaceData(videoBeans);
                    videoAdapter.notifyDataSetChanged();
                }
            }
        });
    }
}
